package com.hyc.controller;

import com.hyc.enmus.ErrorEnum;
import com.hyc.result.RespJson;
import com.hyc.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {
    @Autowired
    private UploadService uploadService;

    @RequestMapping("upload")
    public RespJson upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        RespJson resp = new RespJson();
        try {
            String filePath = uploadService.upload(file, request);
            resp.setData(filePath);
        } catch (Exception e) {
            log.error("上传图片异常", e);
            resp = new RespJson(ErrorEnum.ERROR);
        }
        return resp;
    }
}
